package com.tuo51.childcare.controller;

import com.google.common.collect.ImmutableMap;
import com.tuo51.childcare.domain.expertroom.ExpertHint;
import com.tuo51.childcare.service.ExpertRoomService;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.evaluate.domain.news.ArticleListItem;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.evaluate.service.article.ArticleService;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 首页
 */


@RestController
@Slf4j
@RequestMapping("/papi/home")
@Api(value = "/papi/home", tags = "家长服务号 - 首页")
public class PAPIHomeController {

    @Autowired
    ChildrenService childrenService;

    @Autowired
    ExpertRoomService expertRoomService;

    @Autowired
    ArticleService articleService;

    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/first_step", method = GET)
    public StatusResponse firstStep(@RequestHeader("ticket") String ticket ) {

        Staff staff = cache.getStaff(ticket);

        // children
        Children c = childrenService.defaultChildrenForParent(staff, false);

        // banner
        Page<ArticleListItem> banner = articleService.newsList(false,
                "mp_public",
                null,
                "banner000",
                null,
                new PagerQuery());

        // hint
        ExpertHint hint = expertRoomService.generateHintByReport(staff,null);

        //recommend article
        Page<ArticleListItem> homelist = articleService.newsList(false,
                "mp_public",
                null,
                "home000",
                null,
                new PagerQuery());


        Map ret = null;

        if (c == null) {
            ret = ImmutableMap.of("banner", banner, "hint", hint, "recommends", homelist);
        }else{
            ret = ImmutableMap.of("children", c, "banner", banner, "hint", hint, "recommends", homelist);
        }

        return StatusResponse.success(ret);
    }

}
